package com.yztc.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yztc.chunboapp.MainActivity;
import com.yztc.chunboapp.R;
import com.yztc.utils.DataCleanManager;
import com.yztc.utils.ShareLog;

import cn.bmob.v3.BmobUser;

/**
 * Created by My on 2016/10/20.
 */
public class SettingActivity extends AppCompatActivity {
    //显示缓存大小
    private TextView mTVCacheSize;
    //退出按钮
    private Button mBtnExit;
    //设置的线性布局item
    private LinearLayout mLinePersonnal, mLineSecurity, mLineAddress, mLineService, mLineShare,
            mLineAdvice, mLineHelp, mLineRemove, mLineVersion;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initData();
        //设置监听
        setOnclickListener();
    }

    private void initData() {
        //获取app缓存大小
        mTVCacheSize.setText("缓存大小（"+ DataCleanManager.getTotalCacheSize(getApplication())+")");
    }

    private void setOnclickListener() {
        //点击按钮，退出登录
        mBtnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //清除缓存用户对象
                BmobUser.logOut();
                MainActivity.tag = false;
                finish();
            }
        });

        //点击分享
        mLineShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ShareLog(SettingActivity.this, "分享春播");
            }
        });

        //点击拨打客服电话
        mLineService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 直接连接打电话
                Uri uri = Uri.parse("tel:400-8888-878");
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(intent);
            }
        });

        //点击清除缓存
        mLineRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             AlertDialog.Builder buidler = new AlertDialog.Builder(SettingActivity.this);
             buidler.setTitle("提示");
             buidler.setMessage("确定要清除缓存吗？");
             buidler.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                     DataCleanManager.clearAllCache(getApplication());
                     mTVCacheSize.setText("缓存大小（0.0MB）");
                 }
             });
            buidler.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

                buidler.create().show();
            }

        });
        //点击获取版本信息
        mLineVersion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(SettingActivity.this,
                        "当前版本 Version"+getAppVersionName(SettingActivity.this),Toast.LENGTH_LONG)
                        .show();

            }
        });
    }
    //获取当前版本号
    private  String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
            if (TextUtils.isEmpty(versionName)) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }

    private void initView() {
        mBtnExit = (Button) findViewById(R.id.btn_exit);
        mLinePersonnal = (LinearLayout) findViewById(R.id.line_personal);
        mLineSecurity = (LinearLayout) findViewById(R.id.line_security);
        mLineService = (LinearLayout) findViewById(R.id.line_service);
        mLineAddress = (LinearLayout) findViewById(R.id.line_address);
        mLineService = (LinearLayout) findViewById(R.id.line_service);
        mLineShare = (LinearLayout) findViewById(R.id.line_share);
        mLineAdvice = (LinearLayout) findViewById(R.id.line_share);
        mLineHelp = (LinearLayout) findViewById(R.id.line_help);
        mLineRemove = (LinearLayout) findViewById(R.id.line_remove);
        mLineVersion = (LinearLayout) findViewById(R.id.line_version);
        mTVCacheSize = (TextView) findViewById(R.id.tv_cacheSize);
    }
}
